const path = "/environment_sla/_search";

const getCapacity = (nodeKeyword: string) => {
  const values = {
    size: 0,
    query: {
      prefix: {
        "node.keyword": {
          value: "",
        },
      },
    },
    aggs: {
      cpu: {
        percentiles: {
          field: "util.cpu",
          percents: [80],
        },
      },
      mem: {
        percentiles: {
          field: "util.mem",
          percents: [80],
        },
      },
      disk: {
        percentiles: {
          field: "util.disk",
          percents: [80],
        },
      },
    },
  };
  values.query.prefix["node.keyword"].value = nodeKeyword;
  const data = fetch(path, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(values),
  }).then((response) => response.json());
  return data;
};

//HACK 为了使用前端搜索，这块写死分页第一页，5000 条查询，分页使用前端分页
const fetchTable = (
  nodeKeyword: string,
  pageSize: number = 5000,
  pageCurrent: number = 0
) => {
  const values = {
    from: 0,
    size: 5000,
    _source: [
      "hostname",
      "util.cpu",
      "util.mem",
      "util.disk",
      "generalstatus",
      "personalstatus",
      "servicestatus",
      "cronnum",
      "machinestatus",
    ],
    query: {
      prefix: {
        "node.keyword": {
          value: "",
        },
      },
    },
  };

  values.from = pageCurrent;
  values.size = pageSize;
  values.query.prefix["node.keyword"].value = nodeKeyword;

  const data = fetch(path, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(values),
  }).then((response) => response.json());

  return data;
};

export { getCapacity, fetchTable };
